package cn.fanqinghong.ymcc.service.impl;

import cn.fanqinghong.ymcc.service.ITokenService;
import cn.fanqinghong.ymcc.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * FAN
 * 2022/7/27  16:40
 */
@Service
public class TokenServiceImpl implements ITokenService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    /**
     * 创建防重复token
     * @param courseId
     * @return
     */
    @Override
    public String creatToken(String courseId) {
        //那个人购买的那个课程
        //当前登陆人购买的课程
        Long loginId = 1l;
        String token = StrUtils.getComplexRandomString(10);
        //redis的key伟当前课程id:登陆人id
        redisTemplate.opsForValue().set(
                courseId+":"+loginId,
                token,
                10*60,
                TimeUnit.SECONDS);

        return token;
    }
}
